<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Aug 15, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

/**
 * Abstract class for detecting the AutoRead tool 
 *
 */
class AbstractDOSDetect extends Object {
	
	var $limitRequest; // 1000 request per $timePerLimit
	var $timePerLimit; // 60 second
	

	var $_startTime; // The start time
	var $_numberRequest; // Number of request from start time
	

	/**
	 * Enter description here...
	 *
	 * @param int $limitRequest
	 * @param int $timePerLimit
	 */
	function __construct($limitRequest = 5000, $timePerLimit = 60) {
		$this->limitRequest = $limitRequest;
		$this->timePerLimit = $timePerLimit;
		$this->_loadLimitInfo ();
		$this->_numberRequest ++;
	}
	
	/**
	 * Check if the request is overlimitation
	 *
	 * @return boolean
	 */
	function isOverLimit() {
		if ($this->_numberRequest > $this->limitRequest) {
			return true;
		}
		return false;
	}
	
	/**
	 * load the start time and number of limitation from some where in "memory"
	 *
	 * @abstract 
	 */
	function _loadLimitInfo() {
		die ( "Must implement the function: " . get_class ( $this ) . "::" . __FUNCTION__ );
		// in the sub class must define how to get the $_startTime and $_numberRequest
		//example:
		$this->_startTime = $_SESSION ['DOSDetect_startTime'];
		$this->_numberRequest = $_SESSION ['DOSDetect_numberRequest'];
	}
	
	/**
	 * save the start time and number of limitation into some where in "memory"
	 *
	 * @abstract 
	 */
	function _saveLimitInfo() {
		die ( "Must implement the function: " . get_class ( $this ) . "::" . __FUNCTION__ );
		// in the sub class must define how to get the $_startTime and $_numberRequest
		//example:
		$_SESSION ['DOSDetect_startTime'] = $this->_startTime;
		$_SESSION ['DOSDetect_numberRequest'] = $this->_numberRequest;
	}
	
	/**
	 * Check for DOS or limit the auto getting content tool
	 *
	 */
	function checkDOS() {
		$duration = time () - $this->_startTime;
		if ($this->isOverLimit ()) {
			$delta = $this->timePerLimit + DOS_TIME_WAITING - $duration;
			if ($delta > 0) {
				if (! $this->_doImageValidation ( 20 - ($delta / $duration) )) {
					exit ( 0 );
				}
			}
		}
		if ($duration >= $this->timePerLimit) {
			$this->_startTime = time ();
			$this->_numberRequest = 0;
		}
		
		$this->_saveLimitInfo ();
	}
	
	/**
	 * Do the image validation
	 *
	 * @param int $size
	 * @return boolean
	 */
	function _doImageValidation($size) {
		include dirname ( __FILE__ ) . "/VerifyImage.class.php";
		$vImage = new VerifyImage ( );
		
		// check if there isn't any submit -> print verify form;
		if ($_SERVER ['REQUEST_METHOD'] == 'POST') {
			
			$vImage->loadCodes ();
			if ($vImage->checkCode ()) {
				// if ok
				return true;
			}
		}
		
		if ($_REQUEST ["DOSDetect_image"]) {
			$vImage->gerText ( $_GET ['size'] );
			$vImage->showimage ();
			exit ( 0 );
		}
		
		$size = intval ( $size );
		$size = $size < 6 ? 6 : $size;
		$imgUrl = SYS_ROOT_URL . "?DOSDetect_image=1&size={$size}";
		$codeBox = $vImage->showCodBox ();
		
		include SYS_ROOT_PATH . "templates/system/dosVerify.php";
		return false;
	}
}
?>